package com.oscar.ehome.view.loginview;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.view.welcomeview.LeadActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by 孟琬和 on 2017/6/22.
 * 认证页面
 */

public class ApproveActivity extends BasesActivity implements EasyPermissions.PermissionCallbacks {

    /**
     * 返回键
     */
    @BindView(R.id.titlebar_img_right)
    ImageView titlebarImgRight;
    /**
     * 认证
     */
    @BindView(R.id.approve_buton)
    Button approveButon;
    /**
     * title
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 全局布局ID
     */
    @BindView(R.id.approve_layout)
    LinearLayout approveLayout;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    int camera = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approve);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(ApproveActivity.this).getSystemConfig().getColor()));
        initView();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 初始化数据
     */
    public void initView() {
        titlebarImgRight.setVisibility(View.VISIBLE);
        titlebarImgRight.setImageResource(R.mipmap.tuichu);
        titleTopbar.setText(getString(R.string.idcarrd_approve));

        TintManager.setTint(approveButon, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));

    }

    @OnClick({R.id.titlebar_img_right, R.id.approve_buton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_right://退出账号功能
                this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
                SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
                SystemConfigFactory.getInstance(ApproveActivity.this).getSystemConfig().setIsFirst(true);
                //先退出账号在返回到登录页
                Intent intrnt = new Intent(ApproveActivity.this, LoginActivity.class);
                startActivity(intrnt);
                finish();
                break;
            case R.id.approve_buton:
                isPressmion();
                break;
        }
    }

    /**
     * 查看是否是6。0以上系统
     */
    public void isPressmion() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA,};
        if (Build.VERSION.SDK_INT >= 23) {
            camera = 1;
            if (EasyPermissions.hasPermissions(this, perms)) {//检查是否获取该权限
                Intent intent = new Intent(ApproveActivity.this, CaptureActivity.class);
                intent.putExtra("isIdcard", 1);
                startActivity(intent);
                finish();
            } else {
                //第二个参数是被拒绝后再次申请该权限的解释
                //第三个参数是请求码
                //第四个参数是要申请的权限
                EasyPermissions.requestPermissions(this, "认证身份将要开启拍照权限！", 0, perms);
            }
        } else {
            Intent intent = new Intent(ApproveActivity.this, CaptureActivity.class);
            intent.putExtra("isIdcard", 1);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 判断6。0以下的权限是否禁止
     *
     * @return
     */
    private boolean checkWriteExternalPermission() {
        String permission = "android.permission.CAMERA";
        int res = checkCallingOrSelfPermission(permission);
        return (res == PackageManager.PERMISSION_GRANTED);
    }


    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (camera == 1) {
            Intent intent = new Intent(ApproveActivity.this, CaptureActivity.class);
            intent.putExtra("isIdcard", 1);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this, "为了您能使用相机功能，请开启相机权限！")
                    .setTitle("提示")
                    .setPositiveButton("去设置")
                    .setNegativeButton("取消", null)
                    .setRequestCode(0)
                    .build()
                    .show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
